package com.lewis.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lewis.jpa.entity.Course;
import com.lewis.jpa.entity.Location;
import com.lewis.jpa.entity.Person;
import com.lewis.jpa.entity.TeleNumber;
import com.lewis.jpa.entity.TeleType;
@Service("pilotService")
public class PilotService {

	@Autowired
	private PersonService personService;
	@Autowired
	private MetaTypeService metaTypeService;
	@Autowired
	private CourseService courseService;

	@Transactional
	public void intialData() {
		// add tele type
		TeleType home = new TeleType();
		home.setType("home");

		TeleType work = new TeleType();
		work.setType("work");

		List<TeleType> teleList = new ArrayList<TeleType>();
		teleList.add(home);
		teleList.add(work);

		metaTypeService.createTeleTypes(teleList);

		// add location
		Location shanghai = new Location();
		shanghai.setCity("Shanghai");
		shanghai.setCountry("China");

		Location beaverton = new Location();
		beaverton.setCity("beaverton");
		beaverton.setCountry("America");

		List<Location> listLocation = new ArrayList<Location>();
		listLocation.add(shanghai);
		listLocation.add(beaverton);

		metaTypeService.createLocations(listLocation);

		// add course
		Course en_course = new Course();
		en_course.setCourseName("English");

		Course cn_course = new Course();
		cn_course.setCourseName("Chinese");

		List<Course> courseList = new ArrayList<Course>();
		courseList.add(en_course);
		courseList.add(cn_course);

		courseService.createCourses(courseList);

		// add person
		Person p = new Person();
		p.setFirstname("henry");
		p.setLastname("lian");
		p.setBaseLocation(beaverton);

		TeleNumber num = new TeleNumber();
		num.setNumber("9779984611");
		num.setType(home);
		num.setOwner(p);
		List<TeleNumber> teleNumber = new ArrayList<TeleNumber>();
		teleNumber.add(num);
		p.setNumbers(teleNumber);
		p.setTakingCourse(courseList);

		Person shan = new Person();
		shan.setFirstname("shan");
		shan.setLastname("he");
		shan.setBaseLocation(beaverton);

		TeleNumber snum = new TeleNumber();
		snum.setNumber("13423423");
		snum.setType(home);
		snum.setOwner(shan);
		List<TeleNumber> steleNumber = new ArrayList<TeleNumber>();
		steleNumber.add(snum);
		shan.setNumbers(steleNumber);
		shan.setTakingCourse(courseList);

		Person hank = new Person();
		hank.setFirstname("hank");
		hank.setLastname("lian");
		hank.setBaseLocation(shanghai);

		TeleNumber hnum = new TeleNumber();
		hnum.setNumber("1234611");
		hnum.setType(home);
		hnum.setOwner(hank);
		TeleNumber hnum1 = new TeleNumber();
		hnum1.setNumber("13816981720");
		hnum1.setType(work);
		hnum1.setOwner(hank);
		List<TeleNumber> hteleNumber = new ArrayList<TeleNumber>();
		hteleNumber.add(hnum);
		hteleNumber.add(hnum1);
		hank.setNumbers(hteleNumber);
		hank.setTakingCourse(courseList);

		List<Person> personList = new ArrayList<Person>();
		personList.add(p);
		personList.add(shan);
		personList.add(hank);
		personService.savePersons(personList);
	}

	// @Transactional
	// public void searchCritera() {
	// //search criteria
	// Session session = (Session) baseDAO.getEntityManager().getDelegate();
	// List persons = session.createCriteria(Person.class)
	// .add( Restrictions.like("firstname", "he%") )
	// .list();
	//
	// persons = session.createCriteria(Person.class)
	// .add( Restrictions.like("firstname", "h%") )
	// .createCriteria("numbers")
	// .add( Restrictions.like("number", "%4611") )
	// .list();
	//
	//
	// persons = session.createCriteria(Person.class)
	// .createAlias("numbers", "kt")
	// .createAlias("baseLocation", "mt")
	// .add( Restrictions.like("kt.number", "%720") )
	// .add(Restrictions.eq("mt.city", "Shanghai"))
	// .setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP)
	// .list();
	// System.out.println("end");
	//
	// }

}
